package stbm.guihelpers;

import java.util.LinkedList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import stbm.objects.Ship;
import stbm.objects.TradeGood;

/**
 * Validates different things throughout the application
 * 
 * @author Nirav
 * 
 */
public class Validation {

	/**
	 * Validates different parameters entered during game initialization
	 * 
	 * @param name
	 *            player name
	 * @param pt1
	 *            skill point 1
	 * @param pt2
	 *            skill point 2
	 * @param pt3
	 *            skill point 3
	 * @param pt4
	 *            skill point 4
	 * @return true/false based on whether above stuff is valid or not
	 */
	public static boolean gameInitValidates(String name, int pt1, int pt2,
			int pt3, int pt4, boolean difficulty) {

		if (!checkPlayerName(name)) {
			JOptionPane.showMessageDialog(null,
					"Please enter a valid player name...", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if (!checkPoints(pt1, pt2, pt3, pt4)) {
			JOptionPane.showMessageDialog(null,
					"Something is wrong with your addition", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if (!difficulty) {
			JOptionPane.showMessageDialog(null,
					"Please choose a difficulty level", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}

		return true;

	}

	/**
	 * Checks if the name entered by the player is valid or not. It should not
	 * be an empty string and 2 or more characters
	 * 
	 * @param name
	 *            the name entered by the user
	 * @return true/false based on whether the name is valid or not
	 */
	private static boolean checkPlayerName(String name) {
		if (name.equals("") || name.length() < 2
				|| name.equals("Enter Player Name")) {
			return false;
		}
		return true;
	}

	/**
	 * Checks to see if all the points entered for all the skills add up to 16
	 * and are valid
	 * 
	 * @param pt1
	 *            first point
	 * @param pt2
	 *            second point
	 * @param pt3
	 *            third point
	 * @param pt4
	 *            fourth point
	 * @return true/false based on whether the points are valid or not
	 */
	private static boolean checkPoints(int pt1, int pt2, int pt3, int pt4) {

		/*
		 * None of the points should be negative
		 */
		if (pt1 < 0 || pt2 < 0 || pt3 < 0 || pt4 < 0) {
			return false;
		}

		int sum = pt1 + pt2 + pt3 + pt4;

		/*
		 * The sum of all the points should be 16
		 */
		if (sum != 16) {
			return false;
		}

		return true;

	}

	public static boolean checkCargo(Ship ship, int itemsCount) {

		if (itemsCount > ship.cargoLimit) {

			return false;

		}
		return true;
	}

	//
	public static boolean checkMoney(double price, double credits) {
		if (credits >= price) {
			return true;
		}
		return false;
	}

	public static boolean sellingItemsWithinItemCount(Ship ship, int itemsCount) {

		if (itemsCount > ship.getCargo().size()) {

			return false;

		}
		return true;

	}

	public static boolean enoughItemsAvailable(TradeGood tg) {

		if (tg.quantity <= 0) {

			return false;

		}
		return true;

	}

}
